//
//  CitySelectCtrl.m
//  Traveling
//
//  Created by 康苗 on 15/6/13.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "CitySelectCtrl.h"
#import "CitySelectHeader.h"
#import "CityInfo.h"
#define SearchBarHeight 44
#define MarginLeft  8
#define DescFont    15

@interface CitySelectCtrl ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *allItemArray;
@property (nonatomic, strong) NSArray *itemArray ;
@property (assign, nonatomic)TicketSelectViewClickType type;
@property (nonatomic, copy) void(^result)(NSString *city);

@end

@implementation CitySelectCtrl

-(instancetype)initWithType:(TicketSelectViewClickType)type result:(void (^)(NSString *))result{
    self = [super init];
    if(self){
        self.type = type;
        self.result = result;
    }
    return self;
}

-(NSArray *)itemArray{
    if (!_itemArray) {
        if (self.type==TicketSelectViewClickTypeAir) {
            _allItemArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"planecity.plist" ofType:nil]];
            _itemArray = _allItemArray;
        }else if(self.type == TicketSelectViewClickTypeTrain){
            //火车
            _allItemArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"traincity.plist" ofType:nil]];
            _itemArray = _allItemArray;
        }
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"黄坡智慧旅游";
    CitySelectHeader *headerView = [CitySelectHeader cityHeaderViewWithResult:^(NSString *city) {
        NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"self == %@", city];
        NSArray *childArray1 = [self.allItemArray filteredArrayUsingPredicate:predicate1];
        if (childArray1.count==0) {
            return;
        }
        if (self.result) {
            self.result(city);
            [self setHistoryString:city];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = headerView;
    headerView.searchBar.delegate = self;
}

- (UIView *)createHeadView{
    UIView *view = [[UIView alloc] init];
    return view;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }
    cell.textLabel.text = self.itemArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.result) {
        [self setHistoryString:self.itemArray[indexPath.row]];
        self.result(self.itemArray[indexPath.row]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    NSLog(@"%@",searchText);
    if (ISBLANK(searchText)) {
        self.itemArray = [NSArray arrayWithArray:self.allItemArray];
        [self.tableView reloadData];
    }else{
        NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@", searchText];
        NSArray *childArray1 = [self.allItemArray filteredArrayUsingPredicate:predicate1];
        self.itemArray = childArray1;
        [self.tableView reloadData];
    }
}

-(void)setHistoryString:(NSString *)string{
    if (![string isEqualToString:[DefaultsHelper stringForKey:k_historyLocation1]]) {
        [DefaultsHelper setObject:[DefaultsHelper stringForKey:k_historyLocation1] forKey:k_historyLocation2];
        [DefaultsHelper setObject:string forKey:k_historyLocation1];
    }
}

@end
